//
//  MYXCoverViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/4.
//  Copyright © 2017年 hbjApple. All rights reserved.
//选择男女

#import "MYXCoverViewController.h"
#import "PB_CoverTableViewCell.h"
@interface MYXCoverViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    UIButton *selectedBtn;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, copy)  NSMutableArray *titleArray;

@end

@implementation MYXCoverViewController
#pragma mark - Data
-(NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    
    return _titleArray;
}

- (void)loadShuJu
{
    NSArray *titleArr = @[
                          @"男",
                          @"女",
                         ];
    [self.titleArray addObjectsFromArray:titleArr];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadShuJu];
    [self.view addSubview:self.backgroundView];
    [self.view addSubview:self.tableView];

}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(30,SafeAreaTopHeight+(HEIGHT-SafeAreaTopHeight-50*2-10)/2,WIDTH-60, 50*2+10) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.clipsToBounds = YES;
        _tableView.layer.cornerRadius = 5;
        _tableView.bounces = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
//一次触摸事件发生时,该方法只执行一次
-(void)touchesBegan:(NSSet *)touches
          withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.3 animations:^{
        // 1.隐藏遮盖
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
        
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        //            [standardVC.backgroundView removeFromSuperview];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        
    }];
}

#pragma mark - UITableView datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"PB_CoverTableViewCell";
    PB_CoverTableViewCell *PB_CoverTableVi = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(PB_CoverTableVi == nil){
        PB_CoverTableVi = [[PB_CoverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
   
    PB_CoverTableVi.titileLabel.text =self.titleArray[indexPath.section];
    if (indexPath.row==0&&[PB_CoverTableVi.titileLabel.text isEqualToString:@"男"]) {
        PB_CoverTableVi.selectButton.selected =YES;
    }else{
         PB_CoverTableVi.selectButton.selected =NO;
    }
    
    PB_CoverTableVi.selectB =^(UIButton *btn){
        selectedBtn.selected = NO;
        btn.selected = YES;
        selectedBtn = btn;
        self.selectgestureBlock(self.titleArray[indexPath.section]);
        [UIView animateWithDuration:0.3 animations:^{
            // 1.隐藏遮盖
            self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
            
        } completion:^(BOOL finished) {
            [self.tableView removeFromSuperview];
            [self.backgroundView removeFromSuperview];
            //            [standardVC.backgroundView removeFromSuperview];
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
            
        }];
    };
    return PB_CoverTableVi;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView =[[UIView alloc]init];
    headView.backgroundColor =MYYlineColor;
    if (section==0) {
        headView.frame =CGRectMake(0, 0, WIDTH, 0.1);
    }
       headView.frame =CGRectMake(0, 0, WIDTH, 5);
    
    return headView;
}
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    if (section==0) {
        return 0;
    }
     return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *fooerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.1)];
    fooerView.frame =CGRectMake(0, 0, WIDTH, 0.1);
    fooerView.backgroundColor =MYYlineColor;
    return fooerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArray.count;
}

- (UIView *)backgroundView
{
    if (_backgroundView == nil) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, WIDTH, HEIGHT-SafeAreaTopHeight)];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    }
    return _backgroundView;
}
- (void)reset
{
    self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PB_CoverTableViewCell *cell = (PB_CoverTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    selectedBtn.selected = NO;
    cell.selectButton.selected = YES;
    selectedBtn = cell.selectButton;
    self.selectgestureBlock(self.titleArray[indexPath.section]);
    [UIView animateWithDuration:0.3 animations:^{
        // 1.隐藏遮盖
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        //            [standardVC.backgroundView removeFromSuperview];
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
